<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="30vw" :title="`${drawerProps.title}回答`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="回答图片" prop="fileList">
        <UploadImgs v-model:file-list="drawerProps.row!.fileList" height="140px" width="140px" :prefix-data="QA">
          <template #empty>
            <el-icon><Picture /></el-icon>
            <span>请上传照片</span>
          </template>
          <template #tip> 照片大小不能超过 5M </template>
        </UploadImgs>
      </el-form-item>
      <!-- <el-form-item label="分类" prop="typeId">
        <el-cascader
          v-model="drawerProps.row!.typeId"
          :options="options"
          placeholder="请选择分类"
          clearable
          :props="defaultOptions"
          ref="cascaderRef"
        />
      </el-form-item> -->
      <!-- <el-form-item label="标题" prop="title">
        <el-input v-model="drawerProps.row!.title" placeholder="请填写标题" clearable></el-input>
      </el-form-item> -->
      <el-form-item label="内容" prop="content">
        <el-input
          class="at-h"
          rows="10"
          type="textarea"
          v-model="drawerProps.row!.content"
          placeholder="请填写内容"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="点赞数" prop="likeNumber">
        <el-input v-model="drawerProps.row!.likeNumber" placeholder="请填写点赞数" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="评论数" prop="commentNumber">
        <el-input v-model="drawerProps.row!.commentNumber" placeholder="请填写评论数" clearable></el-input>
      </el-form-item>
      <el-form-item label="上传附件" prop="fileLists">
        <UploadFiles
          v-model:file-list="drawerProps.row!.fileLists"
          width="100%"
          height="auto"
          :file-size="3"
          :file-type="['*']"
          :prefix-data="QA"
        >
          <template #empty>
            <el-icon><Upload /></el-icon>
            <span>请上传附件</span>
          </template>
          <template #tip> 附件不能超过 5M </template>
        </UploadFiles>
      </el-form-item>
      <el-form-item>
        <div class="down-wrap">
          <div class="down-item" v-for="(item, index) in drawerProps.row.fileLists" :key="index">
            <span>{{ item.name }}</span>
            <el-button type="primary" @click="downHandler(item)">下载</el-button>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <!-- <el-form-item v-show="drawerProps.isView" label="修改人" prop="modifiedBy">
        <el-input v-model="drawerProps.row!.modifiedBy" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="修改时间" prop="gmtModified">
        <el-input v-model="drawerProps.row!.gmtModified" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="创建人" prop="createBy">
        <el-input v-model="drawerProps.row!.createBy" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item v-show="drawerProps.isView" label="创建时间" prop="gmtCreate">
        <el-input v-model="drawerProps.row!.gmtCreate" placeholder="" clearable></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive, onMounted } from "vue";
// import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { User, File } from "@/api/interface";
import UploadImgs from "@/components/Upload/Imgs.vue";
import UploadFiles from "@/components/Upload/files.vue";
import { downFiles } from "@/utils";
import { downloadFile } from "@/api/modules/sys";
import { QA } from "@/constants";
let defaultOptions = ref();
const rules = reactive({
  fileList: [{ required: true, message: "请上传图片" }],
  content: [{ required: true, message: "请填写内容" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  getDict: (params: any) => Promise<any>;
  fileList: File.ResFileList[];
  fileLists: File.ResFileList[];
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {
    fileList: [],
    fileLists: []
  }
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  // 保存
  defaultOptions.value = {
    lazy: true,
    label: "name",
    value: "id",
    checkStrictly: true
  };
  // if (params.row.typeId) {
  //   let result = await getParamTreeByChildId({ childId: params.row.typeId });
  //   drawerProps.value.row.typeId = result.data.splice(1);
  // }
};
// 下载
const downHandler = async data => {
  downFiles(await downloadFile(data.url), data.name);
};
// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const answerImgModels = drawerProps.value.row.fileList.map(item => {
        return {
          name: item.originalFileName || item.name,
          path: item.path,
          answerId: drawerProps.value.row.id
        };
      });
      const answerFileModels =
        (drawerProps.value.row.fileLists &&
          drawerProps.value.row.fileLists.map(item => {
            return {
              name: item.originalFileName || item.name,
              path: item.path,
              answerId: drawerProps.value.row.id
            };
          })) ||
        [];
      drawerProps.value.row.answerImgModels = answerImgModels;
      drawerProps.value.row.answerFileModels = answerFileModels;
      // drawerProps.value.row.typeId = drawerProps.value.row.typeId[drawerProps.value.row.typeId.length - 1];
      delete drawerProps.value.row.fileList;
      delete drawerProps.value.row.fileLists;

      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}回答成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
onMounted(async () => {
  // const { data } = await getDict!({ parentId: dict.questionId });
  // options.value = data;
});
defineExpose({
  acceptParams
});
</script>
<style scoped="scss">
.el-textarea__inner {
  height: 80px;
}
.down-wrap {
  width: 100%;
  padding: 0;
}
.down-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  color: #333333;
}
</style>
